<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2006/12/29 13:46:14 $)
// $RCSfile: setup.php,v $ : $Revision: 1.8 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

require_once ("./modules/parser/functions_parser.php");

/**
* Called when the module is to be installed
*/
function _parser_install($action, $page = '') {
	global $_SWIFT, $__LANG;

	if ($action == "getpages")
	{
		return "1";
	} else if ($action == "buildpage") {
		$_SWIFT["query"]["create"]["emailqueues"] = "CREATE TABLE ". TABLE_PREFIX ."emailqueues (
																		emailqueueid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		email VARCHAR(255) NOT NULL DEFAULT '',
																		type ENUM('tickets', 'bugs') NOT NULL DEFAULT 'tickets',
																		fetchtype ENUM('pipe', 'pop3', 'pop3ssl', 'imap', 'imapssl') NOT NULL DEFAULT 'pipe',
																		host VARCHAR(255) NOT NULL DEFAULT '',
																		port VARCHAR(10) NOT NULL DEFAULT '',
																		username VARCHAR(255) NOT NULL DEFAULT '',
																		userpassword VARCHAR(255) NOT NULL DEFAULT '',
																		customfromname VARCHAR(200) NOT NULL DEFAULT '',
																		customfromemail VARCHAR(255) NOT NULL DEFAULT '',
																		priorityid INT UNSIGNED NOT NULL DEFAULT '0',
																		ticketstatusid INT UNSIGNED NOT NULL DEFAULT '0',
																		departmentid INT UNSIGNED NOT NULL DEFAULT '0',
																		prefix VARCHAR(30) NOT NULL DEFAULT '',
																		ticketautoresponder SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		replyautoresponder SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		registrationrequired SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		tgroupid INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY (emailqueueid)
																	);";

		$_SWIFT["query"]["create"]["queuesignatures"] = "CREATE TABLE ". TABLE_PREFIX ."queuesignatures (
																		queuesignatureid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		dateline INT UNSIGNED NOT NULL DEFAULT '0',
																		emailqueueid INT UNSIGNED NOT NULL DEFAULT '0',
																		contents TEXT,
																		PRIMARY KEY (queuesignatureid)
																	);";

		$_SWIFT["query"]["create"]["parserlog"] = "CREATE TABLE ". TABLE_PREFIX ."parserlogs (
																		parserlogid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		typeid INT UNSIGNED NOT NULL DEFAULT '0',
																		ticketmaskid VARCHAR(20) NOT NULL DEFAULT '',
																		dateline INT UNSIGNED NOT NULL DEFAULT '0',
																		emailqueueid INT UNSIGNED NOT NULL DEFAULT '0',
																		logtype ENUM('failure', 'success') NOT NULL DEFAULT 'failure',
																		subject VARCHAR(255) NOT NULL DEFAULT '',
																		fromemail VARCHAR(255) NOT NULL DEFAULT '',
																		toemail VARCHAR(255) NOT NULL DEFAULT '',
																		size INT UNSIGNED NOT NULL DEFAULT '0',
																		description VARCHAR(255) NOT NULL DEFAULT '',
																		parsetimetaken FLOAT UNSIGNED NOT NULL DEFAULT '0',
																		responsetype ENUM('ticket', 'reply', 'bug') NOT NULL DEFAULT 'ticket',
																		ticketpostid INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY (parserlogid),
																		KEY parserlogs2 (ticketpostid),
																		KEY parserlogs3 (dateline)
																	);";

		$_SWIFT["query"]["create"]["parserlogdata"] = "CREATE TABLE ". TABLE_PREFIX ."parserlogdata (
																		parserlogdataid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		parserlogid INT UNSIGNED NOT NULL DEFAULT '0',
																		contents MEDIUMTEXT,
																		PRIMARY KEY (parserlogdataid)
																	);";

		$_SWIFT["query"]["create"]["breaklines"] = "CREATE TABLE ". TABLE_PREFIX ."breaklines (
																		breaklineid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		breakline VARCHAR(255) NOT NULL DEFAULT '',
																		PRIMARY KEY (breaklineid)
																	);";

		$_SWIFT["query"]["create"]["parserbans"] = "CREATE TABLE ". TABLE_PREFIX ."parserbans (
																		parserbanid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		email VARCHAR(255) NOT NULL DEFAULT '',
																		dateline INT UNSIGNED NOT NULL DEFAULT '0',
																		staffid INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY (parserbanid),
																		UNIQUE KEY (email)
																	);";

		$_SWIFT["query"]["create"]["catchallrules"] = "CREATE TABLE ". TABLE_PREFIX ."catchallrules (
																		catchallruleid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		title VARCHAR(255) NOT NULL DEFAULT '',
																		ruleexpr VARCHAR(255) NOT NULL DEFAULT '',
																		emailqueueid INT UNSIGNED NOT NULL DEFAULT '0',
																		dateline INT UNSIGNED NOT NULL DEFAULT '0',
																		sortorder INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY (catchallruleid)
																	);";

		$_SWIFT["query"]["create"]["parserrules"] = "CREATE TABLE ". TABLE_PREFIX ."parserrules (
																		parserruleid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		title VARCHAR(255) NOT NULL DEFAULT '',
																		dateline INT UNSIGNED NOT NULL DEFAULT '0',
																		sortorder INT UNSIGNED NOT NULL DEFAULT '0',
																		ruletype SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		matchtype SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		stopprocessing SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY (parserruleid)
																	);";

		$_SWIFT["query"]["create"]["rulecriteria"] = "CREATE TABLE ". TABLE_PREFIX ."rulecriteria (
																		rulecriteriaid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		parserruleid INT UNSIGNED NOT NULL DEFAULT '0',
																		name VARCHAR(100) NOT NULL DEFAULT '',
																		ruleop SMALLINT(2) UNSIGNED NOT NULL DEFAULT '0',
																		rulematch VARCHAR(255) NOT NULL DEFAULT '',
																		PRIMARY KEY (rulecriteriaid),
																		KEY rulecriteria1 (parserruleid)
																	);";

		$_SWIFT["query"]["create"]["ruleactions"] = "CREATE TABLE ". TABLE_PREFIX ."ruleactions (
																		ruleactionid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		parserruleid INT UNSIGNED NOT NULL DEFAULT '0',
																		name VARCHAR(100) NOT NULL DEFAULT '',
																		typeid INT UNSIGNED NOT NULL DEFAULT '0',
																		typedata MEDIUMTEXT,
																		typechar VARCHAR(255) NOT NULL DEFAULT '',
																		PRIMARY KEY (ruleactionid),
																		KEY ruleactions1 (parserruleid)
																	);";

		$_SWIFT["query"]["create"]["floodhash"] = "CREATE TABLE ". TABLE_PREFIX ."floodhash (
																		floodhashid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		dateline INT UNSIGNED NOT NULL DEFAULT '0',
																		emailhash VARCHAR(32) NOT NULL DEFAULT '',
																		subjecthash VARCHAR(32) NOT NULL DEFAULT '',
																		PRIMARY KEY (floodhashid),
																		KEY floodhash1 (emailhash, subjecthash),
																		KEY floodhash2 (dateline)
																	);";

		// Register the module
		$_SWIFT["query"]["insert"]["settings"][0] = "INSERT INTO `". TABLE_PREFIX ."settings`(`section`, `vkey`, `data`) VALUES ('registeredmodules', '". MODULE_PARSER ."', '1')";

		$_SWIFT["query"]["insert"]["breaklines"][] = "INSERT INTO `". TABLE_PREFIX ."breaklines`(`breakline`) VALUES ('----- Original Message -----')";
		$_SWIFT["query"]["insert"]["breaklines"][] = "INSERT INTO `". TABLE_PREFIX ."breaklines`(`breakline`) VALUES ('-----Original Message-----')";
		$_SWIFT["query"]["insert"]["breaklines"][] = "INSERT INTO `". TABLE_PREFIX ."breaklines`(`breakline`) VALUES ('<!-- Break Line -->')";
		$_SWIFT["query"]["insert"]["breaklines"][] = "INSERT INTO `". TABLE_PREFIX ."breaklines`(`breakline`) VALUES ('====== Please reply above this line ======')";
		$_SWIFT["query"]["insert"]["breaklines"][] = "INSERT INTO `". TABLE_PREFIX ."breaklines`(`breakline`) VALUES ('_____')";

		$_SWIFT["query"]["insert"]["cron"][] = "INSERT INTO `". TABLE_PREFIX ."cron` (`nextrun`, `lastrun`, `chour`, `cminute`, `cday`, `module`, `action`, `autorun`, `name`) VALUES ('". time() ."', '". time() ."', '0', '10', '0', 'parser', 'cron', '0', 'parser');";

		$_SWIFT["query"]["insert"]["cron"][] = "INSERT INTO `". TABLE_PREFIX ."cron` (`nextrun`, `lastrun`, `chour`, `cminute`, `cday`, `module`, `action`, `autorun`, `name`) VALUES ('". time() ."', '". time() ."', '0', '0', '-1', 'parser', 'cleanup', '1', 'parsercleanup');";

		/**
		* ###############################################
		* BUILD THE REQUIRED CACHES
		* ###############################################
		*/
		execAllQueries();

		// Queue Cache
		rebuildQueueCache();

		// Breakline Cache
		rebuildBreakLineCache();
	}
}

/**
* Uninstall Routine
*/
function _parser_uninstall()
{
	global $_SWIFT;

	$_SWIFT["query"]["drop"]["emailqueues"] = "DROP TABLE `". TABLE_PREFIX ."emailqueues`;";

	$_SWIFT["query"]["drop"]["queuesignatures"] = "DROP TABLE `". TABLE_PREFIX ."queuesignatures`;";

	$_SWIFT["query"]["drop"]["parserlog"] = "DROP TABLE `". TABLE_PREFIX ."parserlogs`;";

	$_SWIFT["query"]["drop"]["parserlogdata"] = "DROP TABLE `". TABLE_PREFIX ."parserlogdata`;";

	$_SWIFT["query"]["drop"]["breaklines"] = "DROP TABLE `". TABLE_PREFIX ."breaklines`;";

	$_SWIFT["query"]["drop"]["parserbans"] = "DROP TABLE `". TABLE_PREFIX ."parserbans`;";

	$_SWIFT["query"]["drop"]["catchallrules"] = "DROP TABLE `". TABLE_PREFIX ."catchallrules`;";

	$_SWIFT["query"]["drop"]["parserrules"] = "DROP TABLE `". TABLE_PREFIX ."parserrules`;";

	$_SWIFT["query"]["drop"]["rulecriteria"] = "DROP TABLE `". TABLE_PREFIX ."rulecriteria`;";

	$_SWIFT["query"]["drop"]["ruleactions"] = "DROP TABLE `". TABLE_PREFIX ."ruleactions`;";

	$_SWIFT["query"]["delete"]["cron"] = "DELETE FROM `". TABLE_PREFIX ."cron` WHERE `module` = 'parser';";

	$_SWIFT["query"]["delete"]["settings"][0] = "DELETE FROM `". TABLE_PREFIX ."settings` WHERE `section` = 'registeredmodules' AND `vkey` = '". MODULE_PARSER ."';";
}
?>